package com.smart.dao;

import com.smart.domain.CalenderA;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;


@Repository
public class CalenderAdao {
    private JdbcTemplate jdbcTemplate;

    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    //插入日历数据
    public int  addCalen (CalenderA calender)
    {
      String sql = "insert into user_calender (user_term,user_week,user_startday,user_stopday) values(?,?,?,?)";
      Object[] params =new Object[]{calender.getTerm(),calender.getWeek(),calender.getStartday(),calender.getStopday()};
      return jdbcTemplate.update(sql,params);
    }


    //根据学期开始时间删除
    public int deleteCalen(CalenderA calenderA)
    {
        String sql="delete from user_calender where user_startday=?";
        return jdbcTemplate.update(sql,calenderA.getStartday());
    }


    //修改日历
    public  int updateCalen(CalenderA calenderA)
    {
        String sql ="update user_calender set user_term=?,user_week=?,user_startday=?,user_stopday=?";
        return jdbcTemplate.update(sql,calenderA.getTerm(),calenderA.getWeek(),calenderA.getStartday(),calenderA.getStopday());
    }

    //查询日历
    public void querycalen(CalenderA calenderA){

        String sql ="select * from user_calender where user_startday=?";
        CalenderA calenderA1=jdbcTemplate.queryForObject(sql, new CalenderRowMapper(),calenderA.getStartday());
        System.out.println(calenderA1);
    }

    public void querycalenlist(CalenderA calenderA){
        String sql ="select * from user_calender";
        List<CalenderA> calenderAS =jdbcTemplate.query(sql, new CalenderRowMapper());
        System.out.println(calenderAS);
    }



}
class CalenderRowMapper implements RowMapper<CalenderA> {
    @Override
    public CalenderA mapRow(ResultSet rs, int i) throws SQLException {

        //1 从结果集里得到数据
        int term =rs.getInt("user_term");
        int week = rs.getInt("user_week");

        Date startday= rs.getDate("user_startday");

        Date stopday = rs.getDate("user_stopday");


        //2 把得到数据封装到数据里面去
        CalenderA calenderA =new CalenderA();
        calenderA.setWeek(week);
        calenderA.setTerm(term);
        calenderA.setStartday(startday);
        calenderA.setStopday(stopday);
        return calenderA;
    }
}

